﻿using System.ComponentModel;
using System.Runtime.CompilerServices;
using CliCpp.Donet.Models;
using CliCpp.Donet.Utils;

namespace CliCpp.Donet.Base;

public class BaseClass : INotifyPropertyChanged
{
    protected readonly DateTime constructorDateTime = DateTime.Now;
    public readonly int HashCode;

    public readonly string TypeName = string.Empty;

    public BaseClass()
    {
        TypeName = GetType().Name;
        HashCode = GetHashCode();
        LogWarn("Constructor,[RoomsLifecycleMonitor]");
    }

    public virtual string KeyWord => $"{TypeName}<{HashCode}>";

    public event PropertyChangedEventHandler? PropertyChanged;

    ~BaseClass()
    {
        LogWarn("Destruction,[RoomsLifecycleMonitor]");
    }

    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public void OnPropertyChanged<T>(ref T propertyValue, T newValue, [CallerMemberName] string propertyName = null)
    {
        propertyValue = newValue;
        OnPropertyChanged(propertyName);
    }

    protected void LogError(string message, [CallerMemberName] string callerMemberName = null,
        [CallerFilePath] string callerFilePath = null, [CallerLineNumber] int callerLineNumber = -1)
    {
        var log = $"{KeyWord},{message}";

        Log.Instance.Error(log, callerMemberName, callerFilePath, callerLineNumber);
    }

    protected void LogError(string message, LogTransInfo logTransInfo,
        [CallerMemberName] string callerMemberName = null, [CallerFilePath] string callerFilePath = null,
        [CallerLineNumber] int callerLineNumber = -1)
    {
        var log = $"{KeyWord},{message}";
        Log.Instance.Error(log, logTransInfo, callerMemberName, callerFilePath, callerLineNumber);
    }

    protected void LogInfo(string message, LogTransInfo logTransInfo, [CallerMemberName] string callerMemberName = null,
        [CallerFilePath] string callerFilePath = null, [CallerLineNumber] int callerLineNumber = -1)
    {
        var log = $"{KeyWord},{message}";
        Log.Instance.Info(log, logTransInfo, callerMemberName, callerFilePath, callerLineNumber);
    }

    protected void LogInfo(string message, [CallerMemberName] string callerMemberName = null,
        [CallerFilePath] string callerFilePath = null, [CallerLineNumber] int callerLineNumber = -1)
    {
        var log = $"{KeyWord},{message}";
        Log.Instance.Info(log, callerMemberName, callerFilePath, callerLineNumber);
    }

    protected void LogWarn(string message, [CallerMemberName] string callerMemberName = null,
        [CallerFilePath] string callerFilePath = null, [CallerLineNumber] int callerLineNumber = -1)
    {
        var log = $"{KeyWord},{message}";
        Log.Instance.Warn(log, callerMemberName, callerFilePath, callerLineNumber);
    }

    protected void LogWarn(string message, LogTransInfo logTransInfo, [CallerMemberName] string callerMemberName = null,
        [CallerFilePath] string callerFilePath = null, [CallerLineNumber] int callerLineNumber = -1)
    {
        var log = $"{KeyWord},{message}";
        Log.Instance.Warn(log, logTransInfo, callerMemberName, callerFilePath, callerLineNumber);
    }

    protected void LogDebug(string message, LogTransInfo logTransInfo,
        [CallerMemberName] string callerMemberName = null, [CallerFilePath] string callerFilePath = null,
        [CallerLineNumber] int callerLineNumber = -1)
    {
        var log = $"{KeyWord},{message}";
        Log.Instance.Debug(log, logTransInfo, callerMemberName, callerFilePath, callerLineNumber);
    }
}